﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.member
{
    public partial class article_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo == null)
            {
                Response.Redirect("/login.aspx");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(article_add));
        }
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string OnSave(string json)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo != null)
            {
                try
                {
                    var dicProj = JsonHelper.JSONToObject<Dictionary<string, object>>(json);
                    var description = Server.HtmlEncode(dicProj["description"].ToString()).Replace("/", "") ;
                    dicProj.Remove("description");
                    dicProj.Add("description",description);
                    dicProj.Add("client_id", userInfo.Id);
                    if (dicProj["newsId"].ToString() == "")
                    {
                        //添加
                        int count = NewsInfoBll.InsertNewsInfo(dicProj, CommonConst.DB_Name);
                        //添加积分(添加一条内容+10分)
                        ClientInfoBll.UpdateClientPoints(userInfo.Id, CommonConst.gNewsPoints, "+", CommonConst.DB_Name);
                        if (count > 0)
                        {
                            jsonResult.Add("isreload", true);
                            jsonResult.Add("msg", "信息添加成功!");
                        }
                        else
                        {
                            jsonResult.Add("isreload", false);
                            jsonResult.Add("msg", "信息添加失败!");
                        }
                    }
                    else
                    {
                        //修改
                        var proid = EncryptHelper.DecryptString(dicProj["newsId"].ToString());
                        dicProj.Remove("newsId");
                        dicProj.Add("newsId", proid);
                        NewsInfoBll.UpdateNewsInfo(dicProj, CommonConst.DB_Name);
                        jsonResult.Add("isreload", true);
                        jsonResult.Add("msg", "信息修改成功!");
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //获取文章分类
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetNewsType()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=\"\">--请选择--</option>");
            try
            {
                var dt = NewsInfoBll.GetNewsType("0", CommonConst.DB_Name);
                foreach (DataRow row in dt.Rows)
                {
                    builder.AppendFormat("<option value=\"{0}\">{1}</option>", row["NC_Code"], row["NC_Name"]);
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", builder.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //获取商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetNewsInfo(string newsId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo != null)
            {
                try
                {
                    newsId = EncryptHelper.DecryptString(newsId);
                    var tb = NewsInfoBll.GetNewsById(newsId, userInfo.Id, CommonConst.DB_Name);
                    if (tb.Rows.Count != 0)
                    {
                        jsonResult.Add("isreload", true);
                        jsonResult.Add("NC_Code", tb.Rows[0]["NC_Code"]);
                        jsonResult.Add("News_Title", tb.Rows[0]["News_Title"]);
                        jsonResult.Add("News_Picture", tb.Rows[0]["News_Picture"]);
                        jsonResult.Add("News_Note", tb.Rows[0]["News_Note"]);
                        jsonResult.Add("description", tb.Rows[0]["News_Description"]);
                    }
                    else
                    {
                        jsonResult.Clear();
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "该信息不存在或者已被删除！");
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }

            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
